package com.sww.pj.sys.service.impl;

import java.util.List;

import com.sww.pj.common.utils.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.sww.pj.common.annotation.RequiredLog;
import com.sww.pj.common.exception.ServiceException;
import com.sww.pj.common.pojo.CheckBox;
import com.sww.pj.common.pojo.PageObject;
import com.sww.pj.sys.dao.RoleDao;
import com.sww.pj.sys.dao.RoleMenuDao;
import com.sww.pj.sys.pojo.Role;
import com.sww.pj.sys.pojo.RoleMenu;
import com.sww.pj.sys.service.RoleService;

@Service
public class RoleServiceImpl implements RoleService {

	@Autowired
	private RoleDao roleDao;
	@Autowired
	private RoleMenuDao roleMenuDao;

	@Override
	@RequiresPermissions("sww:role:find")
	@RequiredLog("查询角色")
	public PageObject<Role> findPageObjects(String name, Long pageCurrent) {
		//1.参数有效性校验
		if(pageCurrent==null||pageCurrent<1)
			throw new IllegalArgumentException("页码值无效");
		//2.查询总记录数并校验
		long rowCount=roleDao.getRowCount(name);
		if(rowCount==0)
			throw new ServiceException("没有找到对应记录");
		int pageSize=3;
		long startIndex=(pageCurrent-1)*pageSize;
		List<Role> records=
				roleDao.findPageObjects(name, startIndex, pageSize);
		//4.封装查询结果并返回
		return new PageObject<>(rowCount, records, pageSize, pageCurrent);
	}

	@Override
	@RequiresPermissions("sww:role:add")
	@RequiredLog("添加角色")
	public int insertObject(Role role, Integer[] menuIds) {
		if(role==null)
			throw new IllegalArgumentException("保存对象不能为空");
		if(StringUtils.isEmpty(role.getName()))
			throw new IllegalArgumentException("角色名不能为空");
		if(menuIds==null||menuIds.length==0)
			throw new IllegalArgumentException("请为角色分配权限");
		role.setCreatedUser(ShiroUtils.getUsername());
		int rows=roleDao.insertObject(role);
		roleMenuDao.insertObjects(role.getId(), menuIds);
		return rows;
	}
	@Override
	@RequiresPermissions("sww:role:update")
	@RequiredLog("修改角色")
	public int updateObject(Role role, Integer[] menuIds) {
		if(role==null)
			throw new IllegalArgumentException("保存对象不能为空");
		if(StringUtils.isEmpty(role.getName()))
			throw new IllegalArgumentException("角色名不能为空");
		if(menuIds==null||menuIds.length==0)
			throw new IllegalArgumentException("请为角色分配权限");
		role.setModifiedUser(ShiroUtils.getUsername());
		int rows=roleDao.updateObject(role);
		roleMenuDao.deleteObjectsByRoleId(role.getId());
		roleMenuDao.insertObjects(role.getId(), menuIds);
		return rows;
	}

	@Override
	@RequiresPermissions("sww:role:delete")
	@RequiredLog("删除角色")
	public int deleteObject(Integer id) {
		//1.验证数据的合法性
		if(id==null||id<=0)
			throw new IllegalArgumentException("请先选择");
		//3.基于id删除关系数据
		roleMenuDao.deleteObjectsByRoleId(id);
		int rows=roleDao.deleteObject(id);
		if(rows==0)
			throw new ServiceException("此记录可能不存在");
		return rows;
	}

	@Override
	public RoleMenu findObjectById(Integer id) {
		if(id==null||id<1)
			throw new IllegalArgumentException("id值无效");
		RoleMenu rm=roleDao.findObjectById(id);
		return rm;
	}

	@Override
	public List<CheckBox> findRoles() {
		return roleDao.findRoles();
	}



}
